﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
namespace syc.Store
{
    public partial class 废料入库管理 : System.Web.UI.Page
    {
        protected HSSFWorkbook hssfworkbook;
        protected double 废车削;
        protected double 废打包带;
        protected double 废切头;

        protected double 废车削基数;
        protected double 废打包带基数;
        protected double 废切头基数;

        protected int 一分厂套管入库支数;
        protected double 一分厂套管理论重量;

        protected int 二分厂套管入库支数;
        protected double 二分厂套管理论重量;

        protected int 二分厂油管入库支数;
        protected double 二分厂油管理论重量;

        protected int 五分厂油管入库支数;
        protected double 五分厂油管理论重量;

        protected int 三分厂套管接箍数;
        protected double 三分厂套管接箍理论重量;

        protected int 三分厂油管接箍数;
        protected double 三分厂油管接箍理论重量;


        protected double 废车削计划单价;
        protected double 废打包带计划单价;
        protected double 废切头计划单价;

        protected double 废车削实际单价;
        protected double 废打包带实际单价;
        protected double 废切头实际单价;

        protected double 一分厂废打包带实际重量;
        protected double 二分厂废打包带实际重量;
        protected double 五分厂废打包带实际重量;
        protected double 四分厂废打包带实际重量;


        protected string 起始日期;
        protected string 截止日期;

        protected string Notice = "";
        string sql =
@"SELECT t1.ID
,t1.废料ID,
t2.废料名称,
t2.规格
,
	case when t4.ID=1 then '一分厂'
	when t4.ID=2 then '二分厂'
	when t4.ID=3 then '三分厂'
	when t4.ID=4 then '四分厂'
	when t4.ID=5 then '五分厂'	
   
end
 as 供货或使用
,t1.日期
,cast(发货 as tinyint) as 到发
,cast(abs(t1.数量) as float) as 数量
,t1.单价
,t1.单价*t1.数量 as 金额
,t1.保管员
,t1.单据号
,t1.日期 as 入出库日期
,t1.结算方式
FROM 废料到发记录 t1
left outer join 废料 t2 on t2.ID=t1.废料ID
left outer join 生产单位 t4 on t4.ID=t1.入库单位ID
where t1.发货=0
";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.LeiBie.DataBind();
                this.ShouHuoDanWei.DataBind();
                this.Button_Out.Enabled = false;
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
            this.Panel2.Visible = true;
        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.Button_Edit.Enabled = false;
            this.Button_Edit0.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.Button_Out.Enabled = false;

            this.GridView1.SelectedIndex = -1;

            this.Button_Edit.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Button_Delete.CommandName = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_Delete.Enabled = true;

            this.Button_Edit.Enabled = true;
            this.Button_Edit0.Enabled = true;
            string list = this.Button_Delete.CommandName;
            string result = Direct(list);
            string 单据号 = "0";

            if (result == "False")
            {
                //if (单据号 == "")
                //{
                //    this.Button_In.Enabled = true;

                //}
                //else
                //{
                //    this.Button_In.Enabled = true;
                //}
                this.Button_Edit0.Visible = true;
                this.Button_Edit.Visible = false;
                this.Button_Out.Enabled = false;
                //if (this.Panel3.Visible == true)
                //{
                //    this.Panel2.Visible = false;
                //}
                //if (this.Panel3.Visible == false)
                //{
                //    this.Panel2.Visible = true;
                //}

            }
            else
            {
                if (单据号 == "0")
                {
                    this.Button_Out.Enabled = true;
                }
                else
                {
                    this.Button_Out.Enabled = false;
                }
             //   this.Button_In.Enabled = true;
                this.Button_Edit0.Visible = false;
                this.Button_Edit.Visible = true;
                //if (this.Panel3.Visible == true)
                //{
                //    this.Panel2.Visible = false;
                //}
                //if (this.Panel3.Visible == false)
                //{
                //    this.Panel2.Visible = true;
                //}
                // ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料发货登记.aspx?id=" + list + "',600,300);</script>");
            }



        }
        private string GetState()
        {
            string state = "";

            if (this.LeiBie.SelectedItem.Value != "0")
            {
                state += " and t1.废料ID=" + this.LeiBie.SelectedItem.Value;

            }
           
            if (this.ShouHuoDanWei.Text != "0")
            {
                if (this.ShouHuoDanWei.SelectedItem.Text == "一分厂" || this.ShouHuoDanWei.SelectedItem.Text == "二分厂" || this.ShouHuoDanWei.SelectedItem.Text == "三分厂" || this.ShouHuoDanWei.SelectedItem.Text == "四分厂" || this.ShouHuoDanWei.SelectedItem.Text == "五分厂")
                {
                    state += " and t4.ID=" + this.ShouHuoDanWei.Text;
                }
                else
                {
                    state += " and t3.ID=" + this.ShouHuoDanWei.Text;
                }
            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and t1.日期>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and t1.日期<='" + d.ToShortDateString() + "'";
            }

           
           
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {

            string state = this.GetState();

            this.SqlText.Text = sql + state + " order by t1.日期 desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.LeiBie.Text = "0";
            this.Date1.Text = "";
            this.Date2.Text = "";
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.Button_Delete.CommandName;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }



        

        protected void Button_Out_Click(object sender, EventArgs e)
        {
            string list = this.Button_Delete.CommandName;
            if (list == "")
            {
                this.Notice = "请选择出库的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料调拨单.aspx?id=" + list + "',860,440);</script>");
            }
        }

        protected void Button_In_Edit_Click(object sender, EventArgs e)
        {
            //if (this.Panel3.Visible == true)
            //{
            //    this.Panel2.Visible = false;
            //}
            //if (this.Panel3.Visible == false)
            //{
            //    this.Panel2.Visible = true;
            //}
            string list = this.Button_Delete.CommandName;
            string DID = this.GridView1.SelectedDataKey[2].ToString();
            ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料入库单.aspx?id=" + list + "&DID=" + DID + "',685,410);</script>");

        }

        protected void Button_Out_Edit_Click(object sender, EventArgs e)
        {
            //if (this.Panel3.Visible == true)
            //{
            //    this.Panel2.Visible = false;
            //}
            //if (this.Panel3.Visible == false)
            //{
            //    this.Panel2.Visible = true;
            //}
            string list = this.Button_Delete.CommandName;
            string DID = this.GridView1.SelectedDataKey[2].ToString();
            ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料调拨单.aspx?id=" + list + "&DID=" + DID + "',685,410);</script>");
        }

        protected string Direct(string list)
        {
            string result;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter("select 发货 from 废料到发记录 where ID=" + list, Conn);
            sda.Fill(ds);
            result = ds.Tables[0].Rows[0][0].ToString();
            return result;
        }

        protected void Button_To_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = false;
            Response.Redirect("/Store/废品分配表.aspx");
            //this.Panel3.Visible = true;
            //this.Panel4.Visible = true;
        }
       
       



        protected void Button2_Click(object sender, EventArgs e)
        {
            this.Panel2.Visible = true;
            //this.Panel3.Visible = false;
            //this.Panel4.Visible = false;

        }

        protected void Button_In_Click(object sender, EventArgs e)
        {
            string list = this.Button_Delete.CommandName;
            if (list == "")
            {
                this.Notice = "请选择入库的记录。";
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(GetType(), null, "<script>parent.swin2_load('/Store/废料入库单.aspx?id=" + list + "',860,440);</script>");
            }
        }

    }
}
